package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.impl.CrmHelperImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/crmhelper")
public class CrmHelperController {

    @Autowired
    private CrmHelperImpl crmHelper;

    @Autowired
    private UserInfoUtil userInfoUtil;

    @Value("${system.scrm.wxMatrix}")
    private String wxMatrix;

    @Value("${casclient.clientHost}")
    private String host;

    /**
     * 列表
     *
     * @param session
     * @param keyword
     * @param startTime
     * @param endTime
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public Object list(
            HttpServletRequest request,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> record = new HashMap<>();
        record.put("orgId", userinfo.getOrgId());
        record.put("keyword", keyword);
        record.put("startTime", startTime);
        record.put("endTime", endTime);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(crmHelper.list(record));
    }

    /**
     * 单个详情
     *
     * @param session
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Object get(
            HttpServletRequest request,
            @RequestParam(value = "id") String id
    ) throws Exception {
        return crmHelper.get(id);
    }


    /**
     * 新增和编辑
     *
     * @param session
     * @param params
     */
    @PostMapping("/add")
    public void add(
            HttpServletRequest request,
            @RequestBody Map<String, Object> params
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        params.put("orgId", userinfo.getOrgId());
        String token = crmHelper.getTokenByOrgId(userinfo.getOrgId());
        if (token == null) {
            throw GlobalException.build("新增/编辑链接失败，请先前往「设置」关联服务号信息！");
        }
        crmHelper.add(params);
    }

    /**
     * 删除
     *
     * @param session
     * @param params
     */
    @PostMapping("/del")
    public void del(
            HttpServletRequest request,
            @RequestBody Map<String, Object> params
    ) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        params.put("orgId", userinfo.getOrgId());
        crmHelper.del(params);
    }
}
